<!DOCTYPE HTML>
<html lang="zh-CN" class="sidebar-visible no-js rust">
    <head>
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>Hello-Embedded-Linux - Hello-Embedded-Linux</title>


        <!-- Custom HTML head -->

        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="嵌入式Linux系统基本操作介绍。Introduction to basic operation of Embedded-Linux system.">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff" />

        <link rel="icon" href="favicon.svg">
        <link rel="shortcut icon" href="favicon.png">
        <link rel="stylesheet" href="css/variables.css">
        <link rel="stylesheet" href="css/general.css">
        <link rel="stylesheet" href="css/chrome.css">
        <link rel="stylesheet" href="css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="highlight.css">
        <link rel="stylesheet" href="tomorrow-night.css">
        <link rel="stylesheet" href="ayu-highlight.css">

        <!-- Custom theme stylesheets -->

    </head>
    <body>
        <!-- Provide site root to javascript -->
        <script type="text/javascript">
            var path_to_root = "";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "rust";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script type="text/javascript">
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script type="text/javascript">
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('no-js')
            html.classList.remove('rust')
            html.classList.add(theme);
            html.classList.add('js');
        </script>

        <!-- Hide / unhide sidebar before it is displayed -->
        <script type="text/javascript">
            var html = document.querySelector('html');
            var sidebar = 'hidden';
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            }
            html.classList.remove('sidebar-visible');
            html.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded affix "><a href="README_zh.html">Hello-Embedded-Linux</a></li><li class="chapter-item expanded affix "><li class="part-title">认识和操作嵌入式 Linux 系统</li><li class="chapter-item expanded "><a href="系统初始化.html"><strong aria-hidden="true">1.</strong> 系统初始化</a></li><li class="chapter-item expanded "><a href="命令行-vi和shell.html"><strong aria-hidden="true">2.</strong> 命令行 - vi 和 shell</a></li><li class="chapter-item expanded "><a href="系统资源及其配置.html"><strong aria-hidden="true">3.</strong> 系统资源及其配置</a></li><li class="chapter-item expanded "><a href="交叉编译.html"><strong aria-hidden="true">4.</strong> 交叉编译</a></li><li class="chapter-item expanded "><a href="硬件设备操作.html"><strong aria-hidden="true">5.</strong> 硬件设备操作</a></li><li class="chapter-item expanded "><a href="网络操作.html"><strong aria-hidden="true">6.</strong> 网络操作</a></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div>
        </nav>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky bordered">
                    <div class="left-buttons">
                        <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </button>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust (default)</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">Hello-Embedded-Linux</h1>

                    <div class="right-buttons">
                        <a href="print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>
                        <a href="https://github.com/HeyGoda/Hello-Embedded-Linux" title="Git repository" aria-label="Git repository">
                            <i id="git-repository-button" class="fa fa-github"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script type="text/javascript">
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <ul>
<li><a href="#%E6%A6%82%E8%BF%B0">概述</a>
<ul>
<li><a href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C">准备工作</a>
<ul>
<li><a href="#%E4%BD%BF%E7%94%A8-buildroot-%E6%9E%84%E5%BB%BA%E7%B3%BB%E7%BB%9F">使用 Buildroot 构建系统</a></li>
<li><a href="#%E4%BD%BF%E7%94%A8-openwrt-%E6%9E%84%E5%BB%BA%E7%B3%BB%E7%BB%9F">使用 OpenWrt 构建系统</a></li>
<li><a href="#%E4%BD%BF%E7%94%A8-yocto-%E6%9E%84%E5%BB%BA%E7%B3%BB%E7%BB%9F">使用 Yocto 构建系统</a></li>
</ul>
</li>
<li><a href="#%E5%86%85%E5%AE%B9%E7%AE%80%E4%BB%8B">内容简介</a>
<ul>
<li><a href="#%E8%AE%A4%E8%AF%86%E5%92%8C%E6%93%8D%E4%BD%9C%E5%B5%8C%E5%85%A5%E5%BC%8F-linux-%E7%B3%BB%E7%BB%9F">认识和操作嵌入式 Linux 系统</a>
<ul>
<li><a href="#%E7%B3%BB%E7%BB%9F%E5%88%9D%E5%A7%8B%E5%8C%96">系统初始化</a></li>
<li><a href="#%E5%91%BD%E4%BB%A4%E8%A1%8C---vi-%E5%92%8C-shell">命令行 - vi 和 shell</a></li>
<li><a href="#%E7%B3%BB%E7%BB%9F%E8%B5%84%E6%BA%90%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE">系统资源及其配置</a></li>
<li><a href="#%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91">交叉编译</a></li>
<li><a href="#%E7%A1%AC%E4%BB%B6%E8%AE%BE%E5%A4%87%E6%93%8D%E4%BD%9C">硬件设备操作</a></li>
<li><a href="#%E7%BD%91%E7%BB%9C%E6%93%8D%E4%BD%9C">网络操作</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h2 id="概述"><a class="header" href="#概述">概述</a></h2>
<p>嵌入式 Linux 设备多种多样，因 Linux 系统内核、驱动、应用等的自由裁剪和组合，导致不同设备上的软件系统，开发和测试人员在面对一个新设备时难免会感到困惑，在此总结一些通用知识，介绍几个主流嵌入式 Linux 系统之间的异同点，及其跟桌面 Linux 发行版之间的差别，方便熟悉和掌握各种不同的嵌入式 Linux 系统操作方法。</p>
<h3 id="准备工作"><a class="header" href="#准备工作">准备工作</a></h3>
<p>本文档大部分情况下采用 <a href="https://www.qemu.org/">QEMU</a> 来模拟嵌入式设备，如果你已经有嵌入式开发板或设备，则可以结合本文档来实操，而不一定需要安装 QEMU；</p>
<p>QEMU 是一个开源的硬件模拟器和虚拟器，当用作硬件模拟器时，QEMU 可以在一台机器（例如 PC）上运行为另一台机器（例如 ARM 开发板）制作的操作系统和程序；本文档使用 <a href="https://buildroot.org/">Buildroot</a>、<a href="https://openwrt.org/">OpenWrt</a>、<a href="https://www.yoctoproject.org/">Yocto</a> 等构建系统来制作嵌入式 Linux 操作系统及程序，以下在桌面版 Ubuntu 20.4 系统中分别说明其制作方法；</p>
<blockquote>
<p>请先确保系统安装了 QEMU，执行以下命令：</p>
<pre><code class="language-shell">sudo apt install qemu
</code></pre>
</blockquote>
<h4 id="使用-buildroot-构建系统"><a class="header" href="#使用-buildroot-构建系统">使用 Buildroot 构建系统</a></h4>
<p>Buildroot 是一个简单、高效、易用的构建工具，通过交叉编译生成嵌入式 Linux 系统，支持多种嵌入式 Linux 开发板，同时也支持一些 QEMU 板级；以下使用 Buildroot 板级 <code>qemu_arm_vexpress_defconfig</code> 来构建固件，对应 qemu 机器 <code>vexpress-a9</code>；</p>
<p>安装必要的软件（参考链接： <a href="https://buildroot.org/downloads/manual/manual.html#requirement">The Buildroot user manual</a>）：</p>
<pre><code class="language-shell">sudo apt install which sed make binutils build-essential gcc g++ bash patch gzip bzip2 perl tar cpio unzip rsync file bc wget git
</code></pre>
<p>下载 Buildroot 源码并更新到需要的版本：</p>
<pre><code class="language-shell"># 使用git下载源码库
# git clone https://git.busybox.net/buildroot
# 镜像下载
git clone https://gitee.com/mirrors/buildroot.git
cd buildroot
git reset --hard 2021.02.2
</code></pre>
<p>执行默认配置的编译对应板级的固件：</p>
<pre><code class="language-shell"># cd buildroot
make qemu_arm_vexpress_defconfig
make
</code></pre>
<p>编译成功后固件在 <code>output/images/</code> 目录下，如图：</p>
<div align=center><img src="figures/readme/buildroot_make.png"></div>
<p>执行脚本 <code>output/images/start-qemu.sh serial-only</code> 以终端方式（无显示屏）运行编译后的固件，如图：</p>
<div align=center><img src="figures/readme/buildroot_start.png"></div>
<h4 id="使用-openwrt-构建系统"><a class="header" href="#使用-openwrt-构建系统">使用 OpenWrt 构建系统</a></h4>
<p><a href="https://openwrt.org/">OpenWrt</a> 是一个面向嵌入式设备的 Linux 操作系统，最初是针对路由器设备，拥有强大的网络组件和扩展性，具有良好的模块化和可定制性，现在也常用做其他嵌入式设备的 Linux 发行版；</p>
<p>因 <a href="https://git.openwrt.org/">OpenWrt 源码</a> 编译时间较长，且编译过程中可能碰到各种各样的问题，为方便起见，在此使用 <a href="https://openwrt.org/docs/guide-user/virtualization/qemu">官方提供</a> 的 QEMU arm 镜像文件；</p>
<p>下载镜像文件：</p>
<pre><code class="language-shell">mkdir -p openwrt-bin
cd openwrt-bin
# 下载内核镜像
wget https://downloads.openwrt.org/snapshots/targets/armvirt/32/openwrt-armvirt-32-zImage
# 下载根文件系统镜像并解压
wget https://downloads.openwrt.org/snapshots/targets/armvirt/32/openwrt-armvirt-32-rootfs-ext4.img.gz
gunzip -kv openwrt-armvirt-32-rootfs-ext4.img.gz
</code></pre>
<p>如图：</p>
<div align=center><img src="figures/readme/openwrt_down.png"></div>
<p>使用以下命令运行镜像：</p>
<pre><code class="language-shell"># run-qemu-arm_openwrt.sh
qemu-system-arm -nographic -cpu cortex-a7 -smp 1 -M virt -m 256 \
-kernel openwrt-armvirt-32-zImage -append &quot;console=ttyAMA0,115200 rootwait root=fe00&quot; \
-blockdev driver=raw,node-name=hd0,cache.direct=on,file.driver=file,file.filename=openwrt-armvirt-32-rootfs-ext4.img \
-device virtio-blk-pci,drive=hd0
</code></pre>
<p>运行后如图：</p>
<div align=center><img src="figures/readme/openwrt_start.png"></div>
<h4 id="使用-yocto-构建系统"><a class="header" href="#使用-yocto-构建系统">使用 Yocto 构建系统</a></h4>
<p><a href="https://www.yoctoproject.org/">Yocto</a> 是一个开源协作项目，提供了一些列模板、工具和方法，旨在帮助用户创建自定义的基于 Linux 的系统，可用于任何架构的硬件，例如：嵌入式设备、服务器或虚拟环境等；以下使用 beaglebone-yocto 板级来作演示；</p>
<p>参考链接：<a href="https://docs.yoctoproject.org/brief-yoctoprojectqs/index.html">Yocto Project Quick Build — The Yocto Project ® 3.4 documentation</a>, <a href="https://docs.yoctoproject.org/dev-manual/qemu.html">4 Using the Quick EMUlator (QEMU) — The Yocto Project ® 3.4 documentation</a></p>
<p>安装必要的软件：</p>
<pre><code class="language-shell">sudo apt install gawk wget git diffstat unzip texinfo gcc build-essential chrpath socat cpio python3 python3-pip python3-pexpect xz-utils debianutils iputils-ping python3-git python3-jinja2 libegl1-mesa libsdl1.2-dev pylint3 xterm python3-subunit mesa-common-dev zstd liblz4-tool
</code></pre>
<p>下载源码并更新到需要的版本：</p>
<pre><code class="language-shell"># 使用git下载源码库
# git clone git://git.yoctoproject.org/poky
# cd poky
# git reset --hard dunfell-23.0.12
# 若git下载较慢，可直接下载源码包并解压
wget http://downloads.yoctoproject.org/releases/yocto/yocto-3.1.12/poky-dunfell-23.0.12.tar.bz2
tar -xvf poky-dunfell-23.0.12.tar.bz2
</code></pre>
<p>设置环境变量：</p>
<pre><code class="language-shell">cd poky-dunfell-23.0.12
source oe-init-build-env
# 执行上一条命令后，终端将进入build目录，下面的内容都以该目录为基础
</code></pre>
<p>修改编译配置文件 <code>build/conf/local.conf</code>，修改板级为 <code>beaglebone-yocto</code>，将第 31 行：</p>
<pre><code class="language-shell">#MACHINE ?= &quot;beaglebone-yocto&quot;
</code></pre>
<p>改为：</p>
<pre><code class="language-shell">MACHINE ?= &quot;beaglebone-yocto&quot;
</code></pre>
<p>启用并行编译并节省磁盘空间，在文件末尾加入：</p>
<pre><code class="language-shell">INHERIT += &quot;rm_work&quot;
BB_NUMBER_THREADS = &quot;2&quot;
PARALLEL_MAKE = &quot;-j 2&quot;
</code></pre>
<p>如图：</p>
<div align=center><img src="figures/readme/yocto_env.png"></div>
<p>然后开始编译：</p>
<pre><code class="language-shell">bitbake -k core-image-minimal
</code></pre>
<p>编译时间较长，若编译失败，可删除 <code>sstate-cache</code>, <code>cache</code>, <code>tmp</code>目录下的所有文件重新编译，编译成功后的固件在 <code>tmp/deploy/images/beaglebone-yocto/</code> 目录下，如图：</p>
<div align=center><img src="figures/readme/yocto_build.png"></div>
<p>修改 QEMU 运行配置文件 <code>tmp/deploy/images/beaglebone-yocto/core-image-minimal-beaglebone-yocto.qemuboot.conf</code>，加入选项 <code>-nographic</code>，如图：</p>
<div align=center><img src="figures/readme/yocto_qemu_conf.png"></div>
<p>最后执行以下命令运行编译的镜像：</p>
<pre><code>runqemu beaglebone-yocto
</code></pre>
<p>运行后如图：</p>
<div align=center><img src="figures/readme/yocto_start.png"></div>
<h3 id="内容简介"><a class="header" href="#内容简介">内容简介</a></h3>
<h4 id="认识和操作嵌入式-linux-系统"><a class="header" href="#认识和操作嵌入式-linux-系统">认识和操作嵌入式 Linux 系统</a></h4>
<p>从使用者角度介绍嵌入式 Linux 系统基础知识和操作方法；</p>
<h5 id="系统初始化"><a class="header" href="#系统初始化"><a href="%E7%B3%BB%E7%BB%9F%E5%88%9D%E5%A7%8B%E5%8C%96.html">系统初始化</a></a></h5>
<p>介绍用户空间初始化流程；</p>
<h5 id="命令行---vi-和-shell"><a class="header" href="#命令行---vi-和-shell"><a href="%E5%91%BD%E4%BB%A4%E8%A1%8C-vi%E5%92%8Cshell.html">命令行 - vi 和 shell</a></a></h5>
<p>介绍常见命令行使用方法和shell脚本基本语法；</p>
<h5 id="系统资源及其配置"><a class="header" href="#系统资源及其配置"><a href="%E7%B3%BB%E7%BB%9F%E8%B5%84%E6%BA%90%E5%8F%8A%E5%85%B6%E9%85%8D%E7%BD%AE.html">系统资源及其配置</a></a></h5>
<p>介绍系统级资源及其配置方法；</p>
<h5 id="交叉编译"><a class="header" href="#交叉编译"><a href="%E4%BA%A4%E5%8F%89%E7%BC%96%E8%AF%91.html">交叉编译</a></a></h5>
<p>介绍开源软件交叉编译方法；</p>
<h5 id="硬件设备操作"><a class="header" href="#硬件设备操作"><a href="%E7%A1%AC%E4%BB%B6%E8%AE%BE%E5%A4%87%E6%93%8D%E4%BD%9C.html">硬件设备操作</a></a></h5>
<p>介绍常用硬件操作和使用方法；</p>
<h5 id="网络操作"><a class="header" href="#网络操作"><a href="%E7%BD%91%E7%BB%9C%E6%93%8D%E4%BD%9C.html">网络操作</a></a></h5>
<p>介绍网络操作方法；</p>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->

                            <a rel="next" href="系统初始化.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">

                    <a rel="next" href="系统初始化.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>




        <script type="text/javascript">
            window.playground_copyable = true;
        </script>


        <script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="searcher.js" type="text/javascript" charset="utf-8"></script>

        <script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
        <script src="highlight.js" type="text/javascript" charset="utf-8"></script>
        <script src="book.js" type="text/javascript" charset="utf-8"></script>

        <!-- Custom JS scripts -->


    </body>
</html>
